package com.net.oil.car.adapter;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.car.bean.CarBean;
import com.net.oil.http.AppUrl;
import com.net.oil.util.glide.GlideUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：lch
 * 时间：2019/8/11 0011:下午 14:39
 * 邮箱：245850413@qq.com
 * 说明：
 */
public class UseSubsidiaryRecyclerViewAdapter extends RecyclerView.Adapter<UseSubsidiaryRecyclerViewAdapter.ViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private List<CarBean.UseSubsidiaryProductBean> datas;

    public UseSubsidiaryRecyclerViewAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        datas = new ArrayList<>();
    }

    public void updateDatas(List<CarBean.UseSubsidiaryProductBean> datas) {
        this.datas.clear();
        if (datas != null){
            this.datas.addAll(datas);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.use_subsidiary_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        CarBean.UseSubsidiaryProductBean useSubsidiaryProductBean = datas.get(position);
        if (useSubsidiaryProductBean != null){
            String name = useSubsidiaryProductBean.getTitle();
            if (!TextUtils.isEmpty(name)){
                holder.subsidiaryNameTv.setText(name);
            }
            holder.countTv.setText(String.valueOf(useSubsidiaryProductBean.getCount()));
            String imgUrl = "";
            if (useSubsidiaryProductBean.getOilProductBean() != null){
                imgUrl = useSubsidiaryProductBean.getOilProductBean().getImage();
            }
            GlideUtil.getInstance().loadImage(context, holder.productLogo, AppUrl.getImageUrl(imgUrl),
                    R.drawable.product_err);
        }

    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.product_logo)
        ImageView productLogo;
        @BindView(R.id.subsidiary_name_tv)
        TextView subsidiaryNameTv;
        @BindView(R.id.count_tv)
        TextView countTv;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
